/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sj.parksdk.net.manager;


import com.sj.parksdk.net.result.databean.Data_Car;
import com.sj.parksdk.net.result.databean.Data_User;
import com.sj.parksdk.setting.CommonKey;
import com.sj.parksdk.utils.MMKVUtils;

/**
 * token管理器
 *
 * @author tongmm
 * @since 2018/8/6 上午11:45
 */
public class TokenManager {

    private static TokenManager sInstance;

    private String mToken = MMKVUtils.getString(CommonKey.TOKEN_ACCESS_TOKEN,"");

    private String mSign = "";

    private long mTokenExpiresOn = MMKVUtils.getLong(CommonKey.TOKEN_SIGN,0l);;//token 过期时间 秒 时间戳

    private String mRefreshToken = MMKVUtils.getString(CommonKey.TOKEN_REFRESH_TOKEN,"");

    private String mVisitToken="";//访问token
    /**
     * 当前登录的用户
     */
    private Data_User mLoginUser;

    private boolean refreshLoginState = false;

    private Data_Car mDefaultCar;

    private boolean paysuccess = false;//操作支付成功，返回首页需刷新

    private TokenManager() {

    }

    public static TokenManager getInstance() {
        if (sInstance == null) {
            synchronized (TokenManager.class) {
                if (sInstance == null) {
                    sInstance = new TokenManager();
                }
            }
        }
        return sInstance;
    }

    public static TokenManager newInstance(){
        String visitToken = sInstance.mVisitToken;
        String sign = sInstance.mSign;
        sInstance = new TokenManager().setSign(sign).setVisitToken(visitToken).refreshLoginState(true);
        return sInstance;
    }

    public String getVisitToken() {
        return mVisitToken;
    }

    public TokenManager setVisitToken(String mVisitToken) {
        this.mVisitToken = mVisitToken;
        return this;
    }

    public TokenManager setToken(String token) {
        mToken = token;
        return this;
    }

    public String getToken() {
        return mToken;
    }

    public TokenManager setSign(String sign) {
        mSign = sign;
        return this;
    }


    public String getSign() {
        return mSign;
    }

    public Data_User getLoginUser() {
        return mLoginUser;
    }

    public long getTokenExpiresOn() {
        return mTokenExpiresOn;
    }

    public TokenManager setTokenExpiresOn(long mTokenExpiresOn) {
        this.mTokenExpiresOn = mTokenExpiresOn;
        return this;
    }

    public boolean isUserLogined() {
        return MMKVUtils.getBoolean(CommonKey.TOKEN_IS_LOGIN,false);
    }

    public TokenManager setLoginUser(Data_User loginUser) {
        mLoginUser = loginUser;
        return this;
    }

    public String getRefreshToken() {
        return mRefreshToken;
    }

    public TokenManager setRefreshToken(String mRefreshToken) {
        this.mRefreshToken = mRefreshToken;
        return this;
    }

    public Data_Car getDefaultCar() {
        return mDefaultCar;
    }

    public TokenManager setDefaultCar(Data_Car defaultCar) {
        this.mDefaultCar = defaultCar;
        return this;
    }

    public TokenManager refreshLoginState(boolean refresh){
        refreshLoginState = refresh;
        return this;
    }

    public boolean onRefreshLogin(){
        return refreshLoginState;
    }

    public String getAuthorization(){
        return TokenManager.getInstance().getSign()
                + " "
                + TokenManager.getInstance().getToken();
    }
    public String getVisitAuthorization(){
        return TokenManager.getInstance().getSign()
                + " "
                + TokenManager.getInstance().getVisitToken();
    }

    public TokenManager refreshPayState(boolean b){
        paysuccess = b;
        return this;
    }

    public boolean onRefreshPaySuccess(){
        return paysuccess;
    }
}
